﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
//using ImageStatic;

public partial class Services_getPixel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.Charset = "utf-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
        string imageId = "";        
        double x = 0.0;
        double y = 0.0;
        string selectedRS = "";
        string resultXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "\n";
        resultXml += "<Pixels>"+"\n";
        imageId = Request.QueryString[0].ToString();
        if(imageId=="")
        {
            imageId = "BJ_ALL_2003_5_25";
        }
        x = Convert.ToDouble(Request.QueryString[1]);
        y = Convert.ToDouble(Request.QueryString[2]);
        string temp = Request.QueryString[3].ToString();
        string showTXT = "当前信息";//在窗口中显示的信息
        if(temp!="")
        {
            string RS_all = "NDWI,TVI,NDVI,NSI,WI,NDGI,GEMI,EVI,RVI,ARVI,SAVI,MSAVI,DVI,RDVI,SPSI,TVDI,VSWI,XREDEDGE,XVALDEPTH,XWAVELEN,XWAVEWID";
            string[] tmp_array = temp.Split(',');
            if (tmp_array.Length == 2)
            {
                bool bl = RS_all.Contains(tmp_array[0]);
                if (bl == true && tmp_array[1].Contains("Disease")) { selectedRS = tmp_array[0]; showTXT = "病情指数"; }
                if (bl == true && tmp_array[1].Contains("SoilWater")) { selectedRS = tmp_array[0]; showTXT = "土壤含水量"; }
                bl = RS_all.Contains(tmp_array[1]);
                if (bl == true && tmp_array[0].Contains("Disease")) { selectedRS = tmp_array[1]; showTXT = "病情指数"; }
                if (bl == true && tmp_array[0].Contains("SoilWater")) { selectedRS = tmp_array[1]; showTXT = "土壤含水量"; }
            }
            else
            {
                resultXml += "<pixel>请选择相关系数较好的一组参数或者可能运算步骤出错</pixel>" + "\n";
                resultXml += "<showTXT>" + showTXT + "</showTXT>" + "\n";
            }
            
        }
        
            
        //selectedRS = Request.QueryString[3].ToString();
        if(selectedRS=="")
        {
            selectedRS = "NDVI";
            showTXT = "含氮量或水份";
        }
        try
        {      
            string sdeServer = System.Configuration.ConfigurationManager.AppSettings["SDE_SERVER"];
            string sdeInstance = System.Configuration.ConfigurationManager.AppSettings["SDE_INSTANCE"];
            string sdeUser = System.Configuration.ConfigurationManager.AppSettings["SDE_USER"];
            string sdePWD = System.Configuration.ConfigurationManager.AppSettings["SDE_PWD"];
            string sdeDB = "sde";
            string sdeVersion = "SDE.DEFAULT";
            SDEHandle myHandle = new SDEHandle(sdeServer, sdeInstance, sdeUser, sdePWD, sdeDB, sdeVersion);
            imageId = "CT."+selectedRS +"_"+ imageId;//测试，仅仅查NDVI，需要修改
            string pixel = myHandle.GetPixel(imageId, x, y);
            if (pixel != "")
            {
                resultXml += "<pixel>" + pixel + "</pixel>" + "\n";
                resultXml += "<showTXT>" + showTXT + "</showTXT>" + "\n";
            }
            else
            {
                resultXml += "<pixel>表或视图不存在!!可能由于数据库中的数据不全造成，请联系系统管理员！</pixel>" + "\n";
                resultXml += "<showTXT>" + showTXT + "</showTXT>" + "\n";
            }
                
        }
        catch (Exception ex)
        {
            resultXml += "<pixel>Error!" + ex.Message.ToString() + "</pixel>"+"\n";
            resultXml += "<showTXT>" + showTXT + "</showTXT>" + "\n";
        }
        resultXml += "</Pixels>";
        Response.Write(resultXml);
    }
}
